package com.example

import java.io.File

/**
 * 拆分圣经章节的方法
 */
fun main2() {
    val dir = File("out")
    if (!dir.exists()) {
        dir.mkdir()
    }
    val file = File(dir, "temp.md")
    var name = ""
    for (str in File("中文和合本圣经.md").readLines()) {
        if (str.startsWith("旧约全书") || str.startsWith("新约全书")) {
            if (!file.exists()) {
                file.createNewFile()
            } else {
                file.renameTo(File(dir, "$name.md"))
            }
            continue
        }
        if (str.contains(":")) {
            name = str.split(":")[0]
            file.appendText(str + "\n\n")
            continue
        }
    }
}

/**
 * 获取圣经小标题方法
 */
fun main() {
    val fileDirPath = "和合本圣经"
    val volume = "出"
    val number = 0
    File(fileDirPath).walk().maxDepth(1)
        .filter { it.nameWithoutExtension.matches("$volume\\d+".toRegex()) }
        .sortedWith { p0, p1 ->
            val e1 = p0.nameWithoutExtension
            val e2 = p1.nameWithoutExtension
            if (e1.length != e2.length) {
                e1.length - e2.length
            } else {
                e1.compareTo(e2)
            }
        }
        .flatMap { file ->
            file.readLines().filter {
                it.startsWith("##")
            }.map { s ->
                val subtitle = s.substring(3)
                // "${file.nameWithoutExtension}：[$subtitle](${file.name}#$subtitle)"
                "${file.nameWithoutExtension}：$subtitle"
            }
        }.forEachIndexed { i, s ->
            println("${i + 1 + number}. $s")
        }
}
